/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TimeRecording.business;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author cbegg
 */
public class MySQLBackupRestore
{

	private int BUFFER = 10485760;

	private String getData(String host, String port, String user, String password, String db) throws Exception
	{
		Process run = Runtime.getRuntime().exec("mysqldump --host=" + host + " --port=" + port + " --user=" + user + " --password=" + password + " --compact --complete-insert --extended-insert " + "--skip-comments --skip-triggers " + db);
		InputStream in = run.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		StringBuffer temp = new StringBuffer();
		int count;
		char[] cbuf = new char[BUFFER];
		while ((count = br.read(cbuf, 0, BUFFER)) != -1)
		{
			temp.append(cbuf, 0, count);
		}
		br.close();
		in.close();
		return temp.toString();
	}

	private String getRoutine(String host, String port, String user, String password, String db) throws Exception
	{
		Process run = Runtime.getRuntime().exec("mysqldump --host=" + host + " --port=" + port + " --user=" + user + " --password=" + password + " --compact --skip-comments --no-create-info " + "--no-data --routines " + db);
		InputStream in = run.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		StringBuffer temp = new StringBuffer();
		int count;
		char[] cbuf = new char[BUFFER];
		while ((count = br.read(cbuf, 0, BUFFER)) != -1)
		{
			temp.append(cbuf, 0, count);
		}
		br.close();
		in.close();
		return temp.toString();
	}

	/**
	 * Creates a zip file with two SQL files, data.sql and routines.sql
	 * @param host
	 * @param port
	 * @param user
	 * @param password
	 * @param db
	 * @throws java.lang.Exception
	 */
	public void DoBackup(String host, String port, String user, String password, String db) throws Exception
	{
		// Collect data
		byte[] data = getData(host, port, user, password, db).getBytes();
		byte[] routine = getRoutine(host, port, user, password, db).getBytes();

		// Initiating the file stream and the zip stream
		File filedst = new File("example file.zip");
		FileOutputStream dest = new FileOutputStream(filedst);
		ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(dest));

		zip.setMethod(ZipOutputStream.DEFLATED);

		zip.setLevel(Deflater.BEST_COMPRESSION);

		// Write the file
		zip.putNextEntry(new ZipEntry(
				"data.sql"));
		zip.write(data);
		zip.putNextEntry(
				new ZipEntry("routine.sql"));
		zip.write(routine);

		// Close the stream
		zip.close();
		dest.close();
	}

	public void AnotherBackup()
	{
		try
		{
			Runtime.getRuntime().exec("mysqldump -u root -ppword Dname -r C:/backup.sql");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}

	public void AnotherRestore(String databaseName, String userName, String password ) throws java.io.IOException
	{
		String[] executeCmd = new String[]
		{
			"mysql", 
			databaseName, 
			"- -user=",
			userName,
			"--password=",
			password,
			"-e", 
			" source c:/data/sample.sql" 
		};

		Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);		
	}
}